package com.ihd.ihardware.view.tzc.discovery.view;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.ihd.ihardware.R;
import com.ihd.ihardware.common.AppConstans;
import com.ihd.ihardware.databinding.ActivityCommentBinding;
import com.ihd.ihardware.pojo.CommentRes;
import com.ihd.ihardware.pojo.EmptyRes;
import com.ihd.ihardware.view.tzc.discovery.viewmodel.CommentViewModel;

import cn.wowjoy.commonlibrary.base.BaseActivity;
import cn.wowjoy.commonlibrary.base.BaseConsumer;
import cn.wowjoy.commonlibrary.utils.DialogUtils;
import cn.wowjoy.commonlibrary.widget.MDialog;

public class CommentActivity extends BaseActivity<ActivityCommentBinding, CommentViewModel> {
    private String articleId;
    private Intent i;

    @Override
    protected void init(Bundle savedInstanceState) {
        mImmersionBar.keyboardEnable(true).statusBarDarkFont(true).init();

        binding.mtitlebar.setLeftImageResource(R.drawable.ic_back);
        binding.mtitlebar.setLeftClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        binding.mtitlebar.setTitle(getIntent().getStringExtra("title"));
        articleId = getIntent().getStringExtra("articleId");

        setRx(AppConstans.ARTCOMMENTS, new BaseConsumer<CommentRes>() {
            @Override
            protected void onStart() {
            }

            @Override
            protected void onSuccess(CommentRes mLiveData) {
                i = new Intent();
                i.putExtra("count", String.valueOf(mLiveData.getData().getTotal()));
                viewModel.setComments(mLiveData.getData().getList());
            }

            @Override
            protected void onError(Throwable error) {
            }
        });

        setRx(AppConstans.SENDARTCOMMENT, new BaseConsumer<EmptyRes>() {
            @Override
            protected void onStart() {
            }

            @Override
            protected void onSuccess(EmptyRes mLiveData) {
                DialogUtils.successDialog(CommentActivity.this, MDialog.DG_TYPE.SUCCESS, R.drawable.toasticon_suc, "评论成功");
                viewModel.articleCommentList(1, 1000, articleId);
            }

            @Override
            protected void onError(Throwable error) {
            }
        });

        binding.sendIV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (binding.commentET.getText().length() > 0)
                    viewModel.sendArtComment(articleId, binding.commentET.getText().toString());
                binding.commentET.setText("");
            }
        });

        binding.commentRV.setAdapter(viewModel.mDTAdapter);
        viewModel.articleCommentList(1, 1000, articleId);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_comment;
    }

    @Override
    protected Class getViewModelClass() {
        return CommentViewModel.class;
    }

    @Override
    public void onBackPressed() {
        setResult(200, i);
        finish();
    }
}
